<?php

namespace app\admin\validate;

use app\common\model\ProductStock;
use think\Validate;

class Product extends Validate
{

    protected $scene = [
        'EditProduct' => ['product_name','imageurl','describe','type_id','price','mprice','sprice','express_fee','sort','state'],
        'OfflineProduct' => ['product_name','imageurl','sprice','sort','state','describe'],
        'ExchangeProduct' => ['product_name','imageurl','type_id','sort','state','integral','salecount'],
        'NewExchangeProduct' => ['product_name','imageurl','type_id','sort','state','integral','stock_id','stock_name','stock_number','describe','salecount'],
//        'ExchangeProduct' => ['product_name','imageurl','type_id','express_fee','sort','state','integral'],
        'LoveProduct' => ['product_name','imageurl','type_id','sort','state'],
        'TeamProduct' => ['product_name','imageurl','describe','type_id','price','mprice','sprice','sort','state','salecount'],
        'ShowProduct' => ['product_name','imageurl','price','sort','state'],
    ];

    protected $rule =   [
        'product_name'  => 'require',
        'imageurl'  => 'require',
        'describe'  => 'require',
        'type_id'  => 'require|number',
        'price'  => 'require|float|>:0',
        'mprice'  => 'require|float|>:0',
        'sprice'  => 'require|float|>:0',
        'express_fee'  => 'float',
        'sort'  => 'require|number|>:0',
        'state'  => 'require|number|>:0',
        'integral'  => 'float',
        'stock_id' => 'checkStock',
        'stock_name' => 'require',
        'stock_number' => 'require|number',
        'salecount' => 'require|number',
    ];

    protected $message  =   [
        'product_name.require' => '名称必须',
        'describe.require' => '商品详情必须',
        'imageurl.require' => '商品图片必须',
        'type_id.require'   => '类型必须',
        'price.require'   => '原价必须',
        'mprice.require'   => '会员价必须',
        'sprice.require'   => '销售价必须',
        'sort.require'   => '排序必须',
        'state.require'   => '状态必须',
        'type_id.number'   => '类型必须是数字',
        'price.float'   => '价格必须是数字',
        'mprice.float'   => '会员价必须是数字',
        'sprice.float'   => '销售价必须是数字',
        'express_fee.float'   => '邮费必须是数字',
        'sort.number'   => '排序必须是数字',
        'state.number'   => '状态必须是数字',
        'stock_name.require' => '规格名称必须',
        'stock_number.require' => '库存数量必须',
        'stock_number.number' => '库存数量必须是数字',
        'salecount.require' => '销量必须',
        'salecount.number' => '销量必须是数字',
        'integral.float' => '积分必须为数字',
    ];


//
//    protected $scene = [
//        'paixu' => ['sort'],
//    ];
//
//    protected $rule =   [
//        'sort' => 'checkType',
//    ];
//
//    protected $message  =   [
//        'code.checkType' => '排序类型错误',
//    ];
//
//    protected function checkType($value)
//    {
//        $arr = ['desc','asc'];
//        return in_array($value,$arr) == true ? true : '类型错误';
//    }
    protected function checkStock($value,$rule,$data=[])
    {
        if($value != 0){
            $stockModel = new ProductStock();
            $stock = $stockModel->getOneStock($data['product_id'],$data['stock_id']);
            return !empty($stock)? true : '商品id和库存id不匹配';
        }
        return true;
    }
}
